// import otherStore from '..'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI, getUserMessageAPI, getUserPhotoAPI } from '@/api/user'
import { resetRouter } from '@/router'
export default {
  namespaced: true,
  state: {
    token: getToken() || '',
    user_message: {} // 用户信息
  },
  mutations: {
    // 改变token的值
    tokenFn(state, val) {
      setToken(val)
      state.token = val
    },
    // 删除token
    delTokenFn(state) {
      state.token = ''
      removeToken()
    },
    // 改变用户信息
    setUserMessageFn(state, userinfo) {
      state.user_message = userinfo
    },
    delUserMessageFn(state) {
      state.user_message = {}
    }
  },
  actions: {
    // 登录事件获取token
    async loginAction(store, val) {
      const res = await loginAPI(val)
      store.commit('tokenFn', res)
    },
    // 获取用户信息
    async setUserMessageActions(store, val) {
      const usermessage = await getUserMessageAPI()
      // console.log('usermessage', usermessage)
      const userPhoto = await getUserPhotoAPI(usermessage.userId)
      // console.log(userPhoto)
      store.commit('setUserMessageFn', { ...usermessage, ...userPhoto })
      return usermessage.roles
    },
    // 删除token和用户信息
    delAction({ commit }) {
      commit('delTokenFn')
      commit('delUserMessageFn')
      resetRouter()
      commit('em/getRouter', [], { root: true })
    }

  }

}
